/**
 * Created by Xiaoming on 2016/4/14.
 */
function scro(pId){
    var top = $(document).scrollTop();
    var menu = $(pId).find("a");
    if(top == 0){
        menu.each(function (index) {
            if (index == 0) {
                $(this).addClass("active");
            }
        });
    }
}

/*首页滚动监听*/
function scroll(pId,cId,domClass){
    $(window).scroll(function () {
        var items = $(cId).find(domClass);
        var menu = $(pId);
        var top = $(document).scrollTop();
        var currentId = ""; //滚动条现在所在位置的item id
        items.each(function () {
            var m = $(this);
            //注意：m.offset().top代表每一个item的顶部位置
            if (top > m.offset().top-150) {
                currentId =  m.attr("id");
            } else {
                return false;
            }
        });
        var currentLink = menu.find(".active");
        if (currentId && currentLink.attr("class") != currentId) {
            currentLink.removeClass("active");
            menu.find("[class=" + currentId + "]").addClass("active");
        }
    });
}

/*首页滚动监听*/
function click_scroll(hash){
    /*var top = $(document).scrollTop();
     if(top>=100){*/
    var $target = $(hash);
    $target = $target.length && $target || $('[name=' + hash.slice(1) + ']');
    if ($target.length) {
        var targetOffset = $target.offset().top - 50;
        $('html,body').animate({
            scrollTop: targetOffset
        }, 500);
        return false;
    }
}

//顶部固定返回透明度变化
function opacity_top(le) {
    var top = $(document).scrollTop();
    var opacity = top / 100;
    var color = "206,26,33";
    if (top >= 100) {
        opacity = 1
    }
    $(".go_back").css({
        'background': 'rgba(' + color + ',' + opacity + ')'
    });
    $(".go_back_text").css({"opacity":opacity})
    if(top>=131){
        $(le).css({"position":"fixed"})
        $(le).css({"top":44+"px"})
    }
    else{
        $(le).css({"position":"relative"})
        $(le).css({"top":0+"px"})
    }
    $(window).scroll(function () {
        var top = $(document).scrollTop();
        var opacity = top / 100;
        var color = "206,26,33"
        $(".go_back").css({
            'background': 'rgba(' + color + ',' + opacity + ')'
        });
        $(".go_back_text").css({"opacity":opacity})
        if (top >= 100) {
            opacity = 1
        }
        if(top>=131){
            $(le).css({"position":"fixed"})
            $(le).css({"top":44+"px"})
        }
        else{
            $(le).css({"position":"relative"})
            $(le).css({"top":0+"px"})
        }
    })
}
//店铺页面左侧点击右侧跳转到相应的位置
var lastLeftId = '';
function clickGo(clickId) {
    if (lastLeftId != '') {
        lastLeftId.removeAttribute('class', 'store_tag_choose');
    }
    var topMainHei = document.querySelector('.currency').offsetHeight + 40;
    var rightId = document.getElementById('goodList' + clickId);
    var leftId = document.getElementById('leftList' + clickId);
    var contentRight = document.querySelector('.content_right');
    lastLeftId = leftId;
    leftId.setAttribute('class', 'store_tag_choose');
    contentRight.scrollTop = rightId.offsetTop - topMainHei;
}
//店铺页面左侧样式初始化
function initLeftCss(loop) {
    var initLi = $('.content_left').find('li');
    if (initLi.length == 0 && loop < 50) {
        setTimeout(function () {
            loop++;
            initLeftCss(loop);
        }, 200)
    } else {
        if (initLi.length != 0) {
            initLi[0].setAttribute('class', 'store_tag_choose');
            lastLeftId = initLi[0];
        }
    }
}
//店铺页面商品页面高度初始化设置
function setInitStoreBodyHei() {
    var _height = $(window).height() - 40;
    var detailContent = document.querySelector('.detail_content');
    var currency = document.querySelector('.currency').offsetHeight;
    detailContent.style.height = _height + 'px';
    $(window).resize(function () {
        setTimeout(function () {
            _height = $(window).height() - 125;
            currency = document.querySelector('.currency').offsetHeight;
            detailContent.style.height = _height + 'px';
        }, 100)
    });
    var storeMain = document.getElementById('rightMain');
    var modelHtml = document.getElementById('modelHtml');
    var contentLeft = document.querySelector('.content_left');
    storeMain.addEventListener('touchstart', storeMainStart, false);
    contentLeft.addEventListener('touchstart', storeMainStart, false);
    document.addEventListener('scroll', handler, false);
    modelHtml.addEventListener('touchmove', function (e) {
        e.preventDefault()
    }, false);

    storeMain.addEventListener('scroll', storeMainScroll, false);
    function storeMainStart() {
        document.removeEventListener('scroll', handler, false);
        storeMain.removeEventListener('scroll', storeMainScroll, false);
        storeMain.addEventListener('touchmove', storeMainMove, false);
        contentLeft.addEventListener('touchmove', storeMainMove, false);
        storeMain.addEventListener('touchend', storeMainEnd, false);
        contentLeft.addEventListener('touchend', storeMainEnd, false);
    }

    function storeMainMove() {
        var selfId = this.id;
        var scrollPosition = document.body.scrollTop;
        if (scrollPosition < (currency - 40)) {
            storeMain.style.overflow = 'hidden';
            contentLeft.style.overflow = 'hidden';
        } else {
            storeMain.style.overflow = 'auto';
            contentLeft.style.overflow = 'auto';
            if (selfId == 'rightMain') {
                var items = storeMain.children;
                var top = storeMain.scrollTop;
                var currentId = items[0].id.substring(8); //滚动条现在所在位置的item id
                for (var i = 0; i < items.length; i++) {
                    var thisLi = items[i];
                    if (top >= thisLi.offsetTop - 220) {
                        currentId = thisLi.id.substring(8);
                    }
                }
                var leftIdSub = lastLeftId.id.substring(8);
                if (leftIdSub != currentId) {
                    lastLeftId.removeAttribute('class', 'store_tag_choose');
                    lastLeftId = document.getElementById('leftList' + currentId)
                    lastLeftId.setAttribute('class', 'store_tag_choose');
                    contentLeft.scrollTop = lastLeftId.offsetTop - 40 - currency;
                }
            }
        }
    }

    function storeMainEnd() {
        storeMain.removeEventListener('touchmove', storeMainMove, false);
        contentLeft.removeEventListener('touchmove', storeMainMove, false);
        storeMain.removeEventListener('touchend', storeMainEnd, false);
        contentLeft.removeEventListener('touchend', storeMainEnd, false);
        document.addEventListener('scroll', handler, false);
        storeMain.addEventListener('scroll', storeMainScroll, false);
    }

    function handler() {
        var scrollPosition = document.body.scrollTop;
        if (scrollPosition < (currency - 40)) {
            storeMain.style.overflow = 'hidden';
            contentLeft.style.overflow = 'hidden';
        } else {
            storeMain.style.overflow = 'auto';
            contentLeft.style.overflow = 'auto';
        }
    }

    function storeMainScroll() {
        var items = storeMain.children;
        var top = storeMain.scrollTop;
        var currentId = items[0].id.substring(8); //滚动条现在所在位置的item id
        var leftIdSub = lastLeftId.id.substring(8);
        var scrollPosition = document.body.scrollTop;
        for (var i = 0; i < items.length; i++) {
            var thisLi = items[i];
            if (top >= thisLi.offsetTop - 220) {
                currentId = thisLi.id.substring(8);
            }
        }
        if (leftIdSub != currentId) {
            lastLeftId.removeAttribute('class', 'store_tag_choose');
            lastLeftId = document.getElementById('leftList' + currentId);
            lastLeftId.setAttribute('class', 'store_tag_choose');
            contentLeft.scrollTop = lastLeftId.offsetTop - 40 - currency;
        }
    }
}